package cs2340.g21.wms;

import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

/**
 * Displays a list of items 
 * @author Alexander Dodson
 * @version 1.0
 *
 */
public class ItemListActivity extends Activity {

    private Coordinator coordinator;
    private static final String LOG_TAG = "debugger";
    private ListView listview;
    
    /*
     * The item lists. items is the list we're currently viewing,
     * original is the first list we had when we entered this activity 
     */
    private ArrayList<Item> items;
    private ArrayList<Item> original;
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        coordinator = new Coordinator();

        if(getIntent().getExtras() != null){
            coordinator = (Coordinator) getIntent().getExtras().getSerializable("cs2340.g21.wms.coordinator");
            if(getIntent().hasExtra("items")){
                Log.d(LOG_TAG, "using provided list");
                items = (ArrayList<Item>)getIntent().getExtras().get("items");
            }
            if(getIntent().hasExtra("original")){
                original = (ArrayList<Item>)getIntent().getExtras().get("original");
            }
        }
        setContentView(R.layout.activity_user_item_list);
        
        //make a new adapter and set it as the "content creator" for listview
        Item[] temp = new Item[items.size()];
        int x = 0;
        for(Item i : items){
            temp[x++] = i;
        }
        ItemAdapter adapter = new ItemAdapter(this, R.layout.listview_item_row, temp);
        listview = (ListView)findViewById(R.id.listView);
        View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
        listview.addHeaderView(header);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new OnItemClickListener() {
        	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        		Item clickedItem = (Item) listview.getItemAtPosition(position);
        		Intent intent = new Intent();
        		intent.putExtra("cs2340.g21.wms.coordinator", coordinator);
        		intent.putExtra("item", clickedItem);
        		intent.putExtra("items", items);
        		intent.putExtra("original", original);
        		intent.setClass(ItemListActivity.this, ItemPageActivity.class); //takes user to that item's page
        		ItemListActivity.this.onPause();
        		startActivity(intent);
        		ItemListActivity.this.onResume();
        	}
        } );
    }
    
    /**
     * Method called by filter button, opens the filter options page
     * @param view
     */
    public void filterMenu(View view){
        Intent intent = new Intent();
        intent.putExtra("items", items);
        intent.putExtra("original", original);
        intent.putExtra("cs2340.g21.wms.coordinator", coordinator);
        intent.setClass(ItemListActivity.this, FilterActivity.class);
        startActivity(intent);
        finish();
    }
    
    
    /**
     * Method called by the back button, returns to home screen
     * @param view
     */
    public void back(View view){
        Intent intent = new Intent();
        intent.putExtra("cs2340.g21.wms.coordinator", coordinator);
        intent.setClass(ItemListActivity.this, HomeScreenActivity.class);
        startActivity(intent);
        finish();
    }
 
    /**
     * Method called when the hard back key is pressed, returns to home screen
     */
    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra("cs2340.g21.wms.coordinator", coordinator);
        intent.setClass(ItemListActivity.this, HomeScreenActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_user_item_list, menu);
        return true;
    }

}
